package com.ccb.sc.common.controller;

import com.ccb.sc.common.bind.BaseController;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.common.service.IDistrictService;
import com.ccb.sc.common.modules.common.vo.AddressInfoVo;
import com.ccb.sc.common.modules.common.vo.DistrictVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 中国行政区 后台前端控制器
 * </p>
 *
 * @author ken
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/app/district")
@Api(value = "中国行政区", tags = "中国行政区")
public class DistrictController extends BaseController {

    private IDistrictService districtService;

    @GetMapping("/province")
    @ApiOperation(value = "省", response = DistrictVo.class, responseContainer = "list")
    public ResBody<?> getProvince() {
        List<DistrictVo> districtVos = this.districtService.getDistrictTreeByLevel(1);
        return ResBody.success(districtVos);
    }

    @GetMapping("/province-city")
    @ApiOperation(value = "省市", response = DistrictVo.class, responseContainer = "list")
    public ResBody<?> getProvinceCity() {
        List<DistrictVo> districtVos = this.districtService.getDistrictTreeByLevel(2);
        return ResBody.success(districtVos);
    }

    @GetMapping("/province-city-district")
    @ApiOperation(value = "省市区", response = DistrictVo.class, responseContainer = "list")
    public ResBody<?> getProvinceCityDistrict() {
        List<DistrictVo> districtVos = this.districtService.getDistrictTreeByLevel(3);
        return ResBody.success(districtVos);
    }

    @GetMapping("/location")
    @ApiOperation(value = "根据经纬度获取位置信息", response = DistrictVo.class, responseContainer = "list")
    public ResBody<?> location(@ApiParam(value = "纬度", required = true) @RequestParam BigDecimal latitude,
                               @ApiParam(value = "经度", required = true) @RequestParam BigDecimal longitude) {
        AddressInfoVo addressInfoVo = this.districtService.locateDistrict(latitude, longitude);
        return ResBody.success(addressInfoVo);
    }

    @Autowired
    public void setDistrictService(IDistrictService districtService) {
        this.districtService = districtService;
    }
}

